package com.vancouver.knows.ui.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.pangu.util.StringUtil;
import com.vancouver.knows.ui.activity.ImageBrowserActivity;
import com.vancouver.knows.util.AppUtils;

/**
 * 
 * @author cxlin
 * 
 */
public class RichWebView extends WebView {

	public RichWebView(Context context) {
		super(context);
	}

	public RichWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public RichWebView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void loadHtmlText(String htmlText) {
		htmlText = htmlText.replaceAll("\r\n", "<br/>");
		configWebView();
		setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (StringUtil.isEmptyString(url)) {
					return true;
				}

				if (AppUtils.isImg(url)) {
					// 重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边
					Intent intent = new Intent(getContext(), ImageBrowserActivity.class);
					intent.putExtra("url", url);
					getContext().startActivity(intent);

				} else {
					AppUtils.showBrower((Activity) getContext(), url);

				}
				return true;
			}

		});
		htmlText = getHtmlData(htmlText);
		loadData(htmlText, "text/html; charset=UTF-8", null);
		setOnTouchListener(new View.OnTouchListener() {

			public boolean onTouch(View view, MotionEvent event) {
				WebView.HitTestResult hr = ((WebView) view).getHitTestResult();

				if (hr.getType() == HitTestResult.IMAGE_TYPE && event.getAction() == MotionEvent.ACTION_UP) {
					Intent photoIntent = new Intent(getContext(), ImageBrowserActivity.class);
					photoIntent.putExtra("url", hr.getExtra());
					getContext().startActivity(photoIntent);
				}

				return false;
			}
		});
	}

	public void loadURL(String url) {
		configWebView();
		loadUrl(url);
	}

	@SuppressLint("NewApi")
	private void configWebView() {
		this.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		this.getSettings().setJavaScriptEnabled(true);
		this.getSettings().setPluginState(WebSettings.PluginState.ON);
		this.getSettings().setDefaultTextEncodingName("utf-8");
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			this.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
		} else {
			this.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
		}
	}

	private String getHtmlData(String bodyHTML) {
		String head = "<head>"
				+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
				+ "<style type=\"text/css\">img{max-width: 100%; width:auto; height:auto;background-color:#ECECEC;} iframe{max-width: 100%; width:100%; background-color:#ECECEC;}"
				+ "body{line-height:160%}" + "</style>" + "</head>";
		return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
	}

}
